﻿using System.Globalization;
using System.IO;
using System.Web;
using RollToExcel;

namespace OSDK.Rapporter.Excel
{
    public class Export : IHttpHandler
    {
        private FileInfo _reportFile;

        #region IHttpHandler Members

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE"); 
                var excelTemplate = new FileInfo(context.Server.MapPath("MemberImport.xls"));
                var databaseQuery = new FileInfo(context.Server.MapPath("GetMembers.sql"));

                var x = new Exporter(databaseQuery, excelTemplate);
                _reportFile = x.Export();

                context.Response.ContentType = "application/vnd.ms-excel";
                //context.Response.Headers.Add("Content-Disposition", "attachment; filename=MemberExport.xls");
                context.Response.WriteFile(_reportFile.FullName);
                context.Response.Flush();
            }
            finally
            {
                if (_reportFile != null && _reportFile.Exists)
                {
                    _reportFile.Delete();
                }
            }
        }

        public bool IsReusable
        {
            get { return false; }
        }

        #endregion
    }
}